<?php
namespace app\admin\controller;
use think\Db;
class Task extends Base
{
	public function lists(){
        if( is_post() ){
            $page     = input('page');
            $status   = input('status');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    task a
                left join
                    froms b
                on
                    a.froms=b.id
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $status != '100all' ) {
                $sql .= " and a.status = ?";
                $param[] = $status;
            }
            if ( !empty($min_time) ) {
                $sql    .= " and a.create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and a.create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
            $page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by a.id desc limit $start, $page_size";
            $field       = "a.*,b.title";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            $rs = Db::name('ext_field')->select();
            $temp = [];
            foreach($rs as $v){
                $temp[$v['uuid']] = $v['title'];
            }
            
            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                $list[$k]['status']         =  c("type.task_status",$v['status']);
                $list[$k]['froms']         =  $v['title'];
                
                $ext = json_decode($v['fields'],true);
                $fields = "";
                foreach($ext as $key => $val){
                    $fields .=$temp[$key].",";
                }
                $fields = rtrim($fields,",");
                $list[$k]['fields'] = $fields;
                
                //操作按钮
                $list[$k]['action']          =  "";
                if($v['status']!=2){
                    $list[$k]['action']   .=  com_page(['title'=>'删除数据','url'=>url("task/delete"),'ico'=>'del3','post'=>['id'=>$v['id']],'confirm'=>'你确实要删除上传的附件数据吗?']);
                }
            }
            $com_body    = com_build_table($list, array("id",  "fields","attach", "froms","i","j","status","create_time", "action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data,'suc'=>['js_action'=>'function.iphone']]);
        }
        #赋值
        $data = [
            'nav'       => ['上传管理', '列表'],
            'status'    => ['100all'=>'全部状态'] + c('type.task_status'),
        ];
        return view('', $data);
    }
    //删除
    public function delete(){
        if( is_post() ){
            $id = input('id');
            Db::name('user')->where('tid',$id)->delete();
            Db::name('user_history')->where('tid',$id)->delete();
            Db::name('user_tmp')->where('tid',$id)->delete();
            Db::name('task')->where('id',$id)->delete();
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check( $config );
        }
    }
}